import request from "@/utils/request";
import axios from "axios";

export const userInfoService = () =>
    request.get('/api/auth/getInfo')

export const addUser = (user) => request.post('/api/user/addUser',user)

export const userPageList = (query)=> request.get('/api/user/userPageList', {params:query})

export const login = (user) =>
    request.post('/api/user/login', user)

// 用户注册接口
export const register = (userInfo) => {
    return axios.post('/api/user/register', userInfo)
}

//分页查询商品
export const productPageList = (productList) =>
    request.get('/api/product/pageList', {params:productList})

export const addProduct = (product) =>
    request.post('/api/product/addProduct',product)

// 在api/user.js中添加
export const updateProduct = (id, data) => {
    return request.put(`/api/product/update/${id}`, data)
}

export const productPageListWithSort = (query) =>
    request.post('/api/product/pageListWithSort', query)

export const getCartListByUserId = (userId) => {
    return request.get('/api/cart/list', { params: { userId } })
}

export const updateCartQuantity = (cartId, quantity) => {
    return request.put('/api/cart/updateQuantity', { cartId, quantity })
}

export const deleteCartItem = (cartId) => {
    return request.delete(`/api/cart/delete/${cartId}`)
}

export const updateUserInfo = (data) => {
    return request.put('/api/user/update', data)
}

// recharge.js (建议单独文件管理充值API)
export const getRechargeList = () => {
    return request.get('/api/recharge/list')
}

export const approveRecharge = (data) => {
    return request.post('/api/recharge/approve', data)
}

export const submitRechargeRequest = (amount) => {
    const parsed = Number(amount)
    if (isNaN(parsed)) {
        throw new Error('无效的充值金额');
    }
    return request.post('/api/recharge/request', {
        amount: parsed
    });
}

export const addToCart = (productId, quantity) => {
    return request({
        url: '/api/cart/add',
        method: 'post',
        params: {  // 使用params而不是data，对应后端@RequestParam
            productId,
            quantity
        }
    })
}
//lzf的我的地址功能实现
export const addressApi = {
    getUserAddresses(userId) {
        return request({
            url: `/api/deliveryAddress/user/${userId}`,
            method: 'get'
        });
    },

    createAddress(data) {
        return request({
            url: '/api/deliveryAddress',
            method: 'post',
            data
        });
    },

    updateAddress(id, data) {
        return request({
            url: `/api/deliveryAddress/${id}`,
            method: 'put',
            data
        });
    },

    deleteAddress(id) {
        return request({
            url: `/api/deliveryAddress/${id}`,
            method: 'delete'
        });
    },

    setDefaultAddress(addressId, userId) {
        return request({
            url: '/api/deliveryAddress/set-default',
            method: 'post',
            params: { id: addressId, userId }
        });
    }
};


export const resetPassword = (id) => {
    return axios.put(`/api/user/resetPassword/${id}`)
}

export const logicDeleteUser = (id) => {
    return axios.delete(`/api/user/delete/${id}`);
};

export function submitOrder(data) {
    return request.post('api/orderMain/submit', data)
}

export function getUserOrders(userId) {
    return request.get(`api/orderMain/listByUser/${userId}`)
}

export function getUserAddressList(userId) {
    return request.get(`/api/deliveryAddress/list/${userId}`)
}

export function getUserInfoById(id) {
    return request.get(`/api/user/${id}`)
}

export function cancelOrder(orderId) {
    return request({
        url: `/api/orderMain/cancel/${orderId}`,
        method: 'put'
    })
}

export const getOrderList = () => {
    return request.get('/api/orderMain/order')
}

export const deliverOrder = (orderId) => {
    return request.post(`/api/orderMain/orders/deliver/${orderId}`)
}

export const confirmReceive = (orderId) => {
    return request.post(`/api/orderMain/orders/confirm/${orderId}`)
}

export function getUserLevelByConsumption(totalConsumption) {
    if (totalConsumption >= 50000) {
        return { levelId: 5, discountRate: 0.85, pointsMultiplier: 3, canReturnWithoutReview: 1 };
    } else if (totalConsumption >= 20000) {
        return { levelId: 4, discountRate: 0.90, pointsMultiplier: 2, canReturnWithoutReview: 1 };
    } else if (totalConsumption >= 5000) {
        return { levelId: 3, discountRate: 0.95, pointsMultiplier: 1.5, canReturnWithoutReview: 0 };
    } else if (totalConsumption >= 2000) {
        return { levelId: 2, discountRate: 0.98, pointsMultiplier: 1.2, canReturnWithoutReview: 0 };
    } else {
        return { levelId: 1, discountRate: 1, pointsMultiplier: 1, canReturnWithoutReview: 0 };
    }
}

export function updateUserLevelAndBenefits(data) {
    return request.post('/api/user/updateLevel', data)
}

export function applyRefund(orderId) {
    return request.post('/api/orderMain/applyRefund', null, {
        params: { orderId }
    })
}

export function getRefundOrderList() {
    return request.get('/api/orderMain/refundList')
}

export function approveRefund(orderId) {
    return request.post('/api/orderMain/approveRefund', null, {
        params: { orderId }
    })
}

export function rejectRefund(orderId) {
    return request.post('/api/orderMain/rejectRefund', null, {
        params: { orderId }
    })
}
